package com.hmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallAreaMapper;
import com.hmall.mapper.MallShippingMapper;
import com.hmall.model.MallShipping;
import com.hmall.model.ResultInfo;
import com.hmall.vo.HxmallReslultInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ShippingService {

    private final MallShippingMapper shippingMapper;

    private final MallAreaMapper areaMapper;

    @Autowired
    public ShippingService(MallShippingMapper shippingMapper, MallAreaMapper areaMapper) {
        this.shippingMapper = shippingMapper;
        this.areaMapper = areaMapper;
    }

    public ServerResponse add(Integer userId, MallShipping shipping) {

        if (shipping == null) ServerResponse.createByErrorMessage("创建地址失败");
        assert shipping != null;
        shipping.setUserId(userId);

        /*List<MallShipping> shippingList = shippingMapper.selectPartByUserId(userId);
        for (MallShipping mallShipping : shippingList) {
            if (shipping.equals(mallShipping))
                return ServerResponse.createByErrorMessage("地址重复，请重新输入");
        }
*/
        int resultCount = shippingMapper.myInsert(shipping);
        System.out.println("插入完成");
        return ServerResponse.createBySuccess("新建地址成功");
    }

    /**
     * louxinlong
     * 添加发货地址
     * @return
     */
    public ResultInfo insertShipping(Integer userId, MallShipping shipping){
        if (null == shipping){
            return  HxmallReslultInfoVo.build("","发货地址不能空","");
        }
        shipping.setUserId(userId);
        shippingMapper.insert(shipping);
        return HxmallReslultInfoVo.build("200","发货地址添加成功",shipping);
    }


    public ServerResponse findprovince( int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List province=areaMapper.selectProvince();
        PageInfo pageInfo = new PageInfo(province);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse finddetail(int pageNum, int pageSize,Integer id){
        PageHelper.startPage(pageNum,pageSize);
        List detail=areaMapper.selectDetail(id);
        PageInfo pageInfo = new PageInfo(detail);
        return ServerResponse.createBySuccess(pageInfo);
    }


    public ServerResponse<String> del(Integer userId, Integer shippingId){
        int resultCount = shippingMapper.deleteByShippingIdUserId(userId,shippingId);
        if(resultCount > 0){
            return ServerResponse.createBySuccess("删除地址成功");
        }
        return ServerResponse.createByErrorMessage("删除地址失败");
    }


    public ServerResponse update(Integer userId, MallShipping shipping){
        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateByShipping(shipping);
        if(rowCount > 0){
            return ServerResponse.createBySuccess("更新地址成功");
        }
        return ServerResponse.createByErrorMessage("更新地址失败");
    }
    /**
     * 把已默认的取消
     * @param userId
     * @return
     */
    public ResultInfo updateNoDefaultShipping(Integer userId){
        int rowCount = shippingMapper.updateNoDefaultShipping(userId);
        if(rowCount > 0){
            return HxmallReslultInfoVo.build("200","取消默认成功","");
        }
        return HxmallReslultInfoVo.build("405","取消默认失败","");
    }
    /**
     * 更新地址
     * @param userId
     * @param shipping
     * @return
     */
    public ResultInfo updateByUseridDefault(Integer userId, MallShipping shipping){
        if(null==shipping.getId()){
            return HxmallReslultInfoVo.build("406","地址编号找不到","");
        }
        shipping.setUserId(userId);
        int rowCount = shippingMapper.updateDefaultShipping(shipping);
        if(rowCount > 0){
            return HxmallReslultInfoVo.build("200","更新地址成功","");
        }
        return HxmallReslultInfoVo.build("405","更新地址失败","");
    }

    public ServerResponse<MallShipping> select(Integer userId, Integer shippingId){
        MallShipping shipping = shippingMapper.selectByShippingIdUserId(userId,shippingId);
        if(shipping == null){
            return ServerResponse.createByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createBySuccess("更新地址成功",shipping);
    }


    public ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);

        PageInfo pageInfo = new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }
    //根据用户查询发货地址
    public ResultInfo selectShippingList(Integer userId){
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);
        if (null==shippingList||shippingList.size()==0){
            return HxmallReslultInfoVo.build("201","没有此用户发货地址","");
        }
        return HxmallReslultInfoVo.build("200","发货地址查询成功",shippingList);
    }

    public ServerResponse<List<MallShipping>> list(Integer userId) {
        List<MallShipping> shippingList = shippingMapper.selectByUserId(userId);

        return ServerResponse.createBySuccess(shippingList);
    }

    public ServerResponse<MallShipping> selectByShippingId(int shippingId) {
        MallShipping mallShipping = shippingMapper.selectByPrimaryKey(shippingId);

        return ServerResponse.createBySuccess(mallShipping);
    }
    public MallShipping selectByShippingId(Integer shippingId){
        return shippingMapper.selectByPrimaryKey(shippingId);
    }

    public ServerResponse<MallShipping> findShip(Integer shippingId) {

        MallShipping mallShipping = shippingMapper.selectByPrimaryKey(shippingId);
        return ServerResponse.createBySuccess(mallShipping);
    }
}
